package com.example.springbootelasticsearch;

import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
//import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.net.InetAddress;
import java.net.UnknownHostException;

@Configuration
public class ESConfig {

    @Bean
    public TransportClient client() throws UnknownHostException{
        TransportAddress node= new TransportAddress(
                InetAddress.getByName("localhost"),
                9300
        );
        //在elasticsearch5.x版本中TransportAddress是接口，InetSocketTransportAddress是具体实现类,好像es6.x之后就推荐使用high-level的客户端
        /*InetSocketTransportAddress node = new InetSocketTransportAddress(
                InetAddress.getByName("localhost"),
                9300
        );*/
        Settings settings = Settings.builder().put("cluster.name","zplogo").build();
        TransportClient client = new PreBuiltTransportClient(settings);
        client.addTransportAddress(node);
        //这里可以增加多个节点
        return client;

    }

}
